<?php

use Qiniu\Auth;
use Qiniu\Storage\UploadManager;

global $_W, $_GPC;
$op = empty($_GPC['op']) ? 'setting' : $_GPC['op'];
$type = empty($_GPC['type']) ? 'city' : $_GPC['type'];
$oop = empty($_GPC['oop']) ? 'list' : $_GPC['oop'];
$paixu = empty($_GPC['paixu']) ? 1 : $_GPC['paixu'];
if ($op == 'setting') {
    if ($type == 'city') {
        if ($oop == 'list') {
            $contion = "weid = :weid";
            $params = array(':weid' => $_W['uniacid']);

            if ($paixu == 1) {
                $order = " ORDER BY `order` DESC";
            } elseif ($paixu == 2) {
                $order = " ORDER BY `order` ASC";
            }
            $citylist = pdo_fetchall("SELECT * FROM " . tablename($this->t_city) . " WHERE " . $contion . $order, $params);

        } elseif ($oop == 'add') {
            $cityid = $_GPC['cityid'];

            $cityinfo = pdo_fetch('SELECT * FROM ' . tablename($this->t_city) . " WHERE id = :id AND weid = :weid", array(':id' => $cityid, ':weid' => $_W['uniacid']));
            if ($_W['isajax']) {
                echo json_encode($cityinfo);
                exit;
            }
            if (checksubmit('submit')) {
                $data['name'] = trim($_GPC['name']);
                $data['order'] = intval($_GPC['order']);
                $data['status'] = intval($_GPC['status']);
                $data['description'] = trim($_GPC['description']);
                $data['weid'] = intval($_W['uniacid']);
                $data['addtime'] = time();
                if (empty($data['name'])) {
                    message('城市名称不能为空', '', 'error');
                }
                if ($this->userid == $_W['uid']) {
                    message($this->msg);
                }
                if (empty($cityid)) {
                    $res = pdo_insert($this->t_city, $data);
                    if ($res) {
                        $arr = array(
                            'weid' => $_W['uniacid'],
                            'admin_uid' => $_W['uid'],
                            'admin_username' => $_W['username'],
                            'log_type' => 1,
                            'function' => '基本设置->城市设置',
                            'content' => '新增城市:'.trim($_GPC['name'])
                        );
                        $this->addlog($arr);
                    }
                } else {
                    $res = pdo_update($this->t_city, $data, array('weid' => $_W['uniacid'], 'id' => $cityid));
                    if ($res) {
                        $arr = array(
                            'weid' => $_W['uniacid'],
                            'admin_uid' => $_W['uid'],
                            'admin_username' => $_W['username'],
                            'log_type' => 3,
                            'function' => '基本设置->城市设置',
                            'content' => '更改城市信息:'.trim($_GPC['name'])
                        );
                        $this->addlog($arr);
                    }
                }
                if ($res) {
                    message('操作成功', $this->createWebUrl('index', array('op' => 'setting', 'type' => "city", 'oop' => 'list')), 'success');
                } else {
                    message('操作失败', $this->createWebUrl('index', array('op' => 'setting', 'type' => "city", 'oop' => 'list')), 'error');
                }
            }
        } elseif ($oop == 'delete') {
            if ($this->userid == $_W['uid']) {
                message($this->msg);
            }
            $id = intval($_GPC['cityid']);
            $num = pdo_fetch("SELECT * FROM " . tablename($this->t_teacher) . " WHERE cityid = :cityid AND weid = :weid", array(':cityid' => $id, ':weid' => $_W['uniacid']));
            $snum = pdo_fetch("SELECT * FROM " . tablename($this->t_lesson) . " WHERE cityid = :cityid AND weid = :weid", array(':cityid' => $id, ':weid' => $_W['uniacid']));
            if ($num || $snum) {
                itoast('该城市下还有老师和课程，不能删除');
            }
            $title = pdo_get($this->t_city,array('id' => $id),array('name'));
            $res = pdo_delete($this->t_city, array('weid' => $_W['uniacid'], 'id' => $id));
            if ($res) {
                $arr = array(
                    'weid' => $_W['uniacid'],
                    'admin_uid' => $_W['uid'],
                    'admin_username' => $_W['username'],
                    'log_type' => 2,
                    'function' => '基本设置->城市设置',
                    'content' => '删除城市:'.$title['name']
                );
                $this->addlog($arr);
                message('删除成功', $this->createWebUrl('index', array('op' => 'setting', 'type' => 'city')), 'success');
            } else {
                message('删除失败', '', 'error');
            }
        } elseif ($oop == 'info') {
            $cityid = intval($_GPC['cityid']);
            $info = pdo_get($this->t_city, array('id' => $cityid));
            if ($_W['ispost']) {
                $data['address'] = trim($_GPC['address']);
                $data['tel'] = $_GPC['tel'];
                $data['image'] = trim($_GPC['image']);
                $data['description'] = trim($_GPC['description']);
                $data['qq'] = $_GPC['qq'];
                if ($this->userid == $_W['uid']) {
                    message($this->msg);
                }
                $res = pdo_update($this->t_city, $data, array('id' => $cityid));
                if ($res) {
                    $arr = array(
                        'weid' => $_W['uniacid'],
                        'admin_uid' => $_W['uid'],
                        'admin_username' => $_W['username'],
                        'log_type' => 3,
                        'function' => '基本设置->城市设置',
                        'content' => '修改城市设置:'.$info['name']
                    );
                    $this->addlog($arr);
                    message('信息填写成功', $this->createWebUrl('index', array('op' => 'setting', 'type' => 'city')), 'success');
                } else {
                    message('信息填写失败', $this->createWebUrl('index', array('op' => 'setting', 'type' => 'city')), 'error');
                }
            }
        } elseif ($oop == 'changestatus') {
            if ($this->userid == $_W['uid']) {
                $message = array('success' => 0, 'mess' => $this->msg);
                echo json_encode($message);
                exit;
            }
            $id = $_GPC['id'];
            $data['status'] = $_GPC['sta'];
            $info = pdo_get($this->t_city, array('id' => $id, 'weid' => $_W['uniacid']));
            if ($data['status'] == $info['status']) {
                $message = array('success' => 0, '修改失败，请刷新页面后重新修改');
            } else {
                $res = pdo_update($this->t_city, $data, array('id' => $id, 'weid' => $_W['uniacid']));
                if ($res) {
                    $message = array('success' => 1, 'mess' => '修改成功');
                } else {
                    $message = array('success' => 0, 'mess' => '修改失败');
                }
            }
            echo json_encode($message);
            exit;
        } elseif ($oop == 'sort') {
            if ($this->userid == $_W['uid']) {
                $message = array('success' => 0, 'mess' => $this->msg);
                echo json_encode($message);
                exit;
            }
            $id = $_GPC['cityid'];
            $data['order'] = $_GPC['order'];
            $res = pdo_update($this->t_city, $data, array('id' => $id, 'weid' => $_W['uniacid']));
            if ($res) {
                $message = array('success' => 1, 'mess' => '修改成功');
            } else {
                $message = array('success' => 0, 'mess' => '修改失败');
            }
        }
    } elseif ($type == 'class') {
        if ($oop == 'list') {
            $contion = "weid = :weid";
            $params = array(':weid' => $_W['uniacid']);
            if ($paixu == 1) {
                $order = " ORDER BY `order` DESC";
            } elseif ($paixu == 2) {
                $order = " ORDER BY `order` ASC";
            }
            $classlist = pdo_fetchall("SELECT * FROM " . tablename($this->t_class) . " WHERE " . $contion . $order, $params);
            $subjectlist = pdo_fetchall("SELECT id,name FROM " . tablename($this->t_subject) . " WHERE " . $contion . " ORDER BY `order` DESC", $params);
        } elseif ($oop == 'add') {
            $classid = $_GPC['classid'];
            $classinfo = pdo_fetch('SELECT * FROM ' . tablename($this->t_class) . " WHERE id = :id AND weid = :weid", array(':id' => $classid, ':weid' => $_W['uniacid']));
            $classinfo['subjectid'] = unserialize($classinfo['subjectid']);
            if ($_W['isajax']) {
                echo json_encode($classinfo);
                exit;
            }
            if (checksubmit('submit')) {
                $data['name'] = trim($_GPC['name']);
                $data['order'] = intval($_GPC['order']);
                $data['status'] = intval($_GPC['status']);
                $data['weid'] = intval($_W['uniacid']);
                $data['subjectid'] = trim(serialize($_GPC['subjectid']));
                $data['addtime'] = time();
                $data['classyear'] = $_GPC['classyear'];
                if (empty($data['name'])) {
                    message('年级不能为空', '', 'error');
                }
                if ($this->userid == $_W['uid']) {
                    message($this->msg);
                }
                if (empty($classid)) {
                    $res = pdo_insert($this->t_class, $data);
                    if ($res) {
                        $arr = array(
                            'weid' => $_W['uniacid'],
                            'admin_uid' => $_W['uid'],
                            'admin_username' => $_W['username'],
                            'log_type' => 1,
                            'function' => '基本设置->年级设置',
                            'content' => '新增年级:'.trim($_GPC['name'])
                        );
                        $this->addlog($arr);
                    }
                } else {
                    $res = pdo_update($this->t_class, $data, array('weid' => $_W['uniacid'], 'id' => $classid));
                    if ($res) {
                        $arr = array(
                            'weid' => $_W['uniacid'],
                            'admin_uid' => $_W['uid'],
                            'admin_username' => $_W['username'],
                            'log_type' => 3,
                            'function' => '基本设置->年级设置',
                            'content' => '更改年级信息:'.trim($_GPC['name'])
                        );
                        $this->addlog($arr);
                    }
                }
                if ($res) {
                    message('操作成功', $this->createWebUrl('index', array('op' => 'setting', 'type' => "class", 'oop' => 'list')), 'success');
                } else {
                    message('操作失败', $this->createWebUrl('index', array('op' => 'setting', 'type' => "class", 'oop' => 'list')), 'error');
                }
            }
        } elseif ($oop == 'delete') {
            if ($this->userid == $_W['uid']) {
                message($this->msg);
            }
            $id = intval($_GPC['classid']);
            $snum = pdo_fetch("SELECT * FROM " . tablename($this->t_lesson) . " WHERE classid = :classid AND weid = :weid", array(':classid' => $id, ':weid' => $_W['uniacid']));
            if ($snum) {
                itoast('该班级下还有课程，不能删除');
            }
            $title = pdo_get($this->t_class,array('id' => $id),array('name'));
            $res = pdo_delete($this->t_class, array('weid' => $_W['uniacid'], 'id' => $id));
            if ($res) {
                $arr = array(
                    'weid' => $_W['uniacid'],
                    'admin_uid' => $_W['uid'],
                    'admin_username' => $_W['username'],
                    'log_type' => 2,
                    'function' => '基本设置->年级设置',
                    'content' => '删除年级:'.$title['name']
                );
                $this->addlog($arr);
                message('删除成功', $this->createWebUrl('index', array('op' => 'setting', 'type' => 'class')), 'success');
            } else {
                message('删除失败', '', 'error');
            }
        } elseif ($oop == 'changestatus') {
            if ($this->userid == $_W['uid']) {
                $message = array('success' => 0, 'mess' => $this->msg);
                echo json_encode($message);
                exit;
            }
            $id = $_GPC['id'];
            $data['status'] = $_GPC['sta'];
            $info = pdo_get($this->t_class, array('id' => $id, 'weid' => $_W['uniacid']));
            if ($data['status'] == $info['status']) {
                $message = array('success' => 0, '修改失败，请刷新页面后重新修改');
            } else {
                $res = pdo_update($this->t_class, $data, array('id' => $id, 'weid' => $_W['uniacid']));
                if ($res) {
                    $message = array('success' => 1, 'mess' => '修改成功');
                } else {
                    $message = array('success' => 0, 'mess' => '修改失败');
                }
            }
            echo json_encode($message);
            exit;
        }elseif ($oop == 'sort') {
            if ($this->userid == $_W['uid']) {
                $message = array('success' => 0, 'mess' => $this->msg);
                echo json_encode($message);
                exit;
            }
            $id = $_GPC['classid'];
            $data['order'] = $_GPC['order'];
            //var_dump($id); var_dump($data['order']);die;
            $res = pdo_update($this->t_class, $data, array('id' => $id, 'weid' => $_W['uniacid']));
            if ($res) {
                $message = array('success' => 1, 'mess' => '修改成功');
            } else {
                $message = array('success' => 0, 'mess' => '修改失败');
            }
        }
    } elseif ($type == 'subject') {
        if ($oop == 'list') {
            $contion = "weid = :weid";
            $params = array(':weid' => $_W['uniacid']);
            if ($paixu == 1) {
                $order = " ORDER BY `order` DESC";
            } elseif ($paixu == 2) {
                $order = " ORDER BY `order` ASC";
            }
            $subjectlist = pdo_fetchall("SELECT * FROM " . tablename($this->t_subject) . " WHERE " . $contion .$order, $params);
        }elseif ($oop == 'sort') {
            if ($this->userid == $_W['uid']) {
                $message = array('success' => 0, 'mess' => $this->msg);
                echo json_encode($message);
                exit;
            }
            $id = $_GPC['subjectid'];
            $data['order'] = $_GPC['order'];
            $res = pdo_update($this->t_subject, $data, array('id' => $id, 'weid' => $_W['uniacid']));
            if ($res) {
                $message = array('success' => 1, 'mess' => '修改成功');
            } else {
                $message = array('success' => 0, 'mess' => '修改失败');
            }
        } elseif ($oop == 'add') {
            $subjectid = $_GPC['subjectid'];
            $subjectinfo = pdo_fetch('SELECT * FROM ' . tablename($this->t_subject) . " WHERE id = :id AND weid = :weid", array(':id' => $subjectid, ':weid' => $_W['uniacid']));
            if ($_W['isajax']) {
                echo json_encode($subjectinfo);
                exit;
            }
            if (checksubmit('submit')) {
                $data['name'] = trim($_GPC['name']);
                $data['order'] = intval($_GPC['order']);
                $data['status'] = intval($_GPC['status']);
                $data['weid'] = intval($_W['uniacid']);
                if (empty($data['name'])) {
                    message('学科名称不能为空', '', 'error');
                }
                if ($this->userid == $_W['uid']) {
                    message($this->msg);
                }
                if (empty($subjectid)) {
                    $res = pdo_insert($this->t_subject, $data);
                    if ($res) {

                        $arr = array(
                            'weid' => $_W['uniacid'],
                            'admin_uid' => $_W['uid'],
                            'admin_username' => $_W['username'],
                            'log_type' => 1,
                            'function' => '基本设置->学科设置',
                            'content' => '新增学科:'.trim($_GPC['name'])
                        );
                        $this->addlog($arr);
                    }
                } else {
                    $res = pdo_update($this->t_subject, $data, array('weid' => $_W['uniacid'], 'id' => $subjectid));
                    if ($res) {
                        $arr = array(
                            'weid' => $_W['uniacid'],
                            'admin_uid' => $_W['uid'],
                            'admin_username' => $_W['username'],
                            'log_type' => 3,
                            'function' => '基本设置->学科设置',
                            'content' => '更改学科信息:'.trim($_GPC['name'])
                        );
                        $this->addlog($arr);
                    }
                }
                if ($res) {
                    message('操作成功', $this->createWebUrl('index', array('op' => 'setting', 'type' => "subject", 'oop' => 'list')), 'success');
                } else {
                    message('操作失败', $this->createWebUrl('index', array('op' => 'setting', 'type' => "subject", 'oop' => 'list')), 'error');
                }
            }
        } elseif ($oop == 'delete') {
            if ($this->userid == $_W['uid']) {
                message($this->msg);
            }
            $id = intval($_GPC['subjectid']);
            $num = pdo_fetch("SELECT * FROM " . tablename($this->t_teacher) . " WHERE subjectid = :subjectid AND weid = :weid", array(':subjectid' => $id, ':weid' => $_W['uniacid']));
            $snum = pdo_fetch("SELECT * FROM " . tablename($this->t_lesson) . " WHERE subjectid = :subjectid AND weid = :weid", array(':subjectid' => $id, ':weid' => $_W['uniacid']));
            if ($num || $snum) {
                itoast('该学科下还有老师或课程，不能删除');
            }
            $title = pdo_get($this->t_subject,array('id' => $id),array('name'));
            $res = pdo_delete($this->t_subject, array('weid' => $_W['uniacid'], 'id' => $id));
            if ($res) {
                $arr = array(
                    'weid' => $_W['uniacid'],
                    'admin_uid' => $_W['uid'],
                    'admin_username' => $_W['username'],
                    'log_type' => 2,
                    'function' => '基本设置->学科设置',
                    'content' => '删除学科:'.$title['name']
                );
                $this->addlog($arr);
                message('删除成功', $this->createWebUrl('index', array('op' => 'setting', 'type' => 'subject')), 'success');
            } else {
                message('删除失败', '', 'error');
            }
        } elseif ($oop == 'changestatus') {
            if ($this->userid == $_W['uid']) {
                $message = array('success' => 0, 'mess' => $this->msg);
                echo json_encode($message);
                exit;
            }
            $id = $_GPC['id'];
            $data['status'] = $_GPC['sta'];
            $info = pdo_get($this->t_subject, array('id' => $id, 'weid' => $_W['uniacid']));
            if ($data['status'] == $info['status']) {
                $message = array('success' => 0, '修改失败，请刷新页面后重新修改');
            } else {
                $res = pdo_update($this->t_subject, $data, array('id' => $id, 'weid' => $_W['uniacid']));
                if ($res) {
                    $message = array('success' => 1, 'mess' => '修改成功');
                } else {
                    $message = array('success' => 0, 'mess' => '修改失败');
                }
            }
            echo json_encode($message);
            exit;
        }
    } elseif ($type == 'semester') {
        if ($oop == 'list') {
            $contion = "weid = :weid";
            $params = array(':weid' => $_W['uniacid']);
            $semesterlist = pdo_fetchall("SELECT * FROM " . tablename($this->t_semester) . " WHERE " . $contion, $params);
        } elseif ($oop == 'add') {
            $semesterid = $_GPC['semesterid'];
            $semesterinfo = pdo_fetch('SELECT * FROM ' . tablename($this->t_semester) . " WHERE id = :id AND weid = :weid", array(':id' => $semesterid, ':weid' => $_W['uniacid']));
            if ($_W['isajax']) {
                echo json_encode($semesterinfo);
                exit;
            }
            if (checksubmit('submit')) {
                $data['name'] = trim($_GPC['name']);
                $data['status'] = intval($_GPC['status']);
                $data['weid'] = intval($_W['uniacid']);
                $data['addtime'] = time();
                if (empty($data['name'])) {
                    message('学期名称不能为空', '', 'error');
                }
                if ($this->userid == $_W['uid']) {
                    message($this->msg);
                }
                if (empty($semesterid)) {
                    $res = pdo_insert($this->t_semester, $data);
                    if ($res) {

                        $arr = array(
                            'weid' => $_W['uniacid'],
                            'admin_uid' => $_W['uid'],
                            'admin_username' => $_W['username'],
                            'log_type' => 1,
                            'function' => '基本设置->班期设置',
                            'content' => '新增班期:'.trim($_GPC['name'])
                        );
                        $this->addlog($arr);
                    }
                } else {
                    $res = pdo_update($this->t_semester, $data, array('weid' => $_W['uniacid'], 'id' => $semesterid));
                    if ($res) {

                        $arr = array(
                            'weid' => $_W['uniacid'],
                            'admin_uid' => $_W['uid'],
                            'admin_username' => $_W['username'],
                            'log_type' => 3,
                            'function' => '基本设置->班期设置',
                            'content' => '更改班期信息:'.trim($_GPC['name'])
                        );
                        $this->addlog($arr);
                    }
                }
                if ($res) {
                    message('操作成功', $this->createWebUrl('index', array('op' => 'setting', 'type' => "semester", 'oop' => 'list')), 'success');
                } else {
                    message('操作失败', $this->createWebUrl('index', array('op' => 'setting', 'type' => "semester", 'oop' => 'list')), 'error');
                }
            }
        } elseif ($oop == 'delete') {
            if ($this->userid == $_W['uid']) {
                message($this->msg);
            }
            $id = intval($_GPC['semesterid']);
            $snum = pdo_fetch("SELECT * FROM " . tablename($this->t_lesson) . " WHERE semesterid = :semesterid AND weid = :weid", array(':semesterid' => $id, ':weid' => $_W['uniacid']));
            if ($snum) {
                itoast('该班期下还有课程，不能删除');
            }
            $title = pdo_get($this->t_semester,array('id' => $id),array('name'));
            $res = pdo_delete($this->t_semester, array('weid' => $_W['uniacid'], 'id' => $id));
            if ($res) {
                $arr = array(
                    'weid' => $_W['uniacid'],
                    'admin_uid' => $_W['uid'],
                    'admin_username' => $_W['username'],
                    'log_type' => 2,
                    'function' => '基本设置->班期设置',
                    'content' => '删除班期信息:'.$title['name']
                );
                $this->addlog($arr);
                message('删除成功', $this->createWebUrl('index', array('op' => 'setting', 'type' => 'semester')), 'success');
            } else {
                message('删除失败', '', 'error');
            }
        } elseif ($oop == 'changestatus') {
            if ($this->userid == $_W['uid']) {
                $message = array('success' => 0, 'mess' => $this->msg);
                echo json_encode($message);
                exit;
            }
            $id = $_GPC['id'];
            $data['status'] = $_GPC['sta'];
            $info = pdo_get($this->t_semester, array('id' => $id, 'weid' => $_W['uniacid']));
            if ($data['status'] == $info['status']) {
                $message = array('success' => 0, '修改失败，请刷新页面后重新修改');
            } else {
                $res = pdo_update($this->t_semester, $data, array('id' => $id, 'weid' => $_W['uniacid']));
                if ($res) {
                    $message = array('success' => 1, 'mess' => '修改成功');
                } else {
                    $message = array('success' => 0, 'mess' => '修改失败');
                }
            }
            echo json_encode($message);
            exit;
        }
    } elseif ($type == 'voidtype') {
        if ($oop == 'list') {
            $contion = "weid = :weid";
            $params = array(':weid' => $_W['uniacid']);

            $voidtypelist = pdo_fetchall("SELECT * FROM " . tablename($this->t_voidtype) . " WHERE " . $contion, $params);
        } elseif ($oop == 'add') {
            $voidtypeid = $_GPC['voidtypeid'];
            $voidtypeinfo = pdo_fetch('SELECT * FROM ' . tablename($this->t_voidtype) . " WHERE id = :id AND weid = :weid", array(':id' => $voidtypeid, ':weid' => $_W['uniacid']));
            if ($_W['isajax']) {
                echo json_encode($voidtypeinfo);
                exit;
            }
            if (checksubmit('submit')) {
                $data['name'] = trim($_GPC['name']);
                $data['status'] = intval($_GPC['status']);
                $data['weid'] = intval($_W['uniacid']);
                $data['addtime'] = time();
                if (empty($data['name'])) {
                    message('类型名称不能为空', '', 'error');
                }
                if ($this->userid == $_W['uid']) {
                    message($this->msg);
                }
                if (empty($voidtypeid)) {
                    $res = pdo_insert($this->t_voidtype, $data);
                    if ($res) {
                        $arr = array(
                            'weid' => $_W['uniacid'],
                            'admin_uid' => $_W['uid'],
                            'admin_username' => $_W['username'],
                            'log_type' => 1,
                            'function' => '基本设置->视频类型设置',
                            'content' => '新增视频类型:'.trim($_GPC['name'])
                        );
                        $this->addlog($arr);
                    }
                } else {
                    $res = pdo_update($this->t_voidtype, $data, array('weid' => $_W['uniacid'], 'id' => $voidtypeid));
                    if ($res) {
                        $arr = array(
                            'weid' => $_W['uniacid'],
                            'admin_uid' => $_W['uid'],
                            'admin_username' => $_W['username'],
                            'log_type' => 3,
                            'function' => '基本设置->视频类型设置',
                            'content' => '更改视频类型信息:'.trim($_GPC['name'])
                        );
                        $this->addlog($arr);
                    }
                }
                if ($res) {
                    message('操作成功', $this->createWebUrl('index', array('op' => 'setting', 'type' => "voidtype", 'oop' => 'list')), 'success');
                } else {
                    message('操作失败', $this->createWebUrl('index', array('op' => 'setting', 'type' => "voidtype", 'oop' => 'list')), 'error');
                }
            }
        } elseif ($oop == 'delete') {
            if ($this->userid == $_W['uid']) {
                message($this->msg);
            }
            $id = intval($_GPC['voidtypeid']);
            $snum = pdo_fetch("SELECT * FROM " . tablename($this->t_lesson) . " WHERE voidtypeid = :voidtypeid AND weid = :weid", array(':voidtypeid' => $id, ':weid' => $_W['uniacid']));
            if ($snum) {
                itoast('该视频类型下还有课程，不能删除');
            }
            $title = pdo_get($this->t_voidtype,array('id' => $id),array('name'));
            $res = pdo_delete($this->t_voidtype, array('weid' => $_W['uniacid'], 'id' => $id));
            if ($res) {
                $arr = array(
                    'weid' => $_W['uniacid'],
                    'admin_uid' => $_W['uid'],
                    'admin_username' => $_W['username'],
                    'log_type' => 2,
                    'function' => '基本设置->视频类型设置',
                    'content' => '删除视频类型信息:'.$title['name']
                );
                $this->addlog($arr);
                message('删除成功', $this->createWebUrl('index', array('op' => 'setting', 'type' => 'voidtype')), 'success');
            } else {
                message('删除失败', '', 'error');
            }
        } elseif ($oop == 'changestatus') {
            if ($this->userid == $_W['uid']) {
                $message = array('success' => 0, 'mess' => $this->msg);
                echo json_encode($message);
                exit;
            }
            $id = $_GPC['id'];
            $data['status'] = $_GPC['sta'];
            $info = pdo_get($this->t_voidtype, array('id' => $id, 'weid' => $_W['uniacid']));
            if ($data['status'] == $info['status']) {
                $message = array('success' => 0, '修改失败，请刷新页面后重新修改');
            } else {
                $res = pdo_update($this->t_voidtype, $data, array('id' => $id, 'weid' => $_W['uniacid']));
                if ($res) {
                    $message = array('success' => 1, 'mess' => '修改成功');
                } else {
                    $message = array('success' => 0, 'mess' => '修改失败');
                }
            }
            echo json_encode($message);
            exit;
        }
    } elseif ($type == 'level') {
        if ($oop == 'list') {
            $contion = "weid = :weid";
            $params = array(':weid' => $_W['uniacid']);
            $levellist = pdo_fetchall("SELECT * FROM " . tablename($this->t_level) . " WHERE " . $contion, $params);
        } elseif ($oop == 'add') {
            $lvid = $_GPC['lvid'];
            $lvinfo = pdo_fetch('SELECT * FROM ' . tablename($this->t_level) . " WHERE id = :id AND weid = :weid", array(':id' => $lvid, ':weid' => $_W['uniacid']));
            if ($_W['isajax']) {
                echo json_encode($lvinfo);
                exit;
            }
            if (checksubmit('submit')) {
                $data['name'] = trim($_GPC['name']);
                $data['time'] = intval($_GPC['time']);
                $data['status'] = intval($_GPC['status']);
                $data['weid'] = $_W['uniacid'];
                $data['addtime'] = time();
                if (empty($data['name']) || empty($_GPC['time'])) {
                    itoast('等级名称/观看时长不能为空', '', 'error');
                }
                if ($this->userid == $_W['uid']) {
                    message($this->msg);
                }
                if (empty($lvid)) {
                    $res = pdo_insert($this->t_level, $data);
                    if ($res) {
                        $arr = array(
                            'weid' => $_W['uniacid'],
                            'admin_uid' => $_W['uid'],
                            'admin_username' => $_W['username'],
                            'log_type' => 1,
                            'function' => '基本设置->等级设置',
                            'content' => '新增等级:'.trim($_GPC['name'])
                        );
                        $this->addlog($arr);
                    }
                } else {
                    $res = pdo_update($this->t_level, $data, array('weid' => $_W['uniacid'], 'id' => $lvid));
                    if ($res) {
                        $arr = array(
                            'weid' => $_W['uniacid'],
                            'admin_uid' => $_W['uid'],
                            'admin_username' => $_W['username'],
                            'log_type' => 3,
                            'function' => '基本设置->等级设置',
                            'content' => '更改等级信息:'.trim($_GPC['name'])
                        );
                        $this->addlog($arr);
                    }
                }
                if ($res) {
                    message('操作成功', $this->createWebUrl('index', array('op' => 'setting', 'type' => "level", 'oop' => 'list')), 'success');
                } else {
                    message('操作失败', $this->createWebUrl('index', array('op' => 'setting', 'type' => "level", 'oop' => 'list')), 'error');
                }
            }
        } elseif ($oop == 'delete') {
            if ($this->userid == $_W['uid']) {
                message($this->msg);
            }
            $id = intval($_GPC['levelid']);
            $title = pdo_get($this->t_level,array('id' => $id),array('name'));
            $res = pdo_delete($this->t_level, array('weid' => $_W['uniacid'], 'id' => $id));
            if ($res) {
                $arr = array(
                    'weid' => $_W['uniacid'],
                    'admin_uid' => $_W['uid'],
                    'admin_username' => $_W['username'],
                    'log_type' => 2,
                    'function' => '基本设置->等级设置',
                    'content' => '删除等级信息:'.$title['name']
                );
                $this->addlog($arr);
                message('删除成功', $this->createWebUrl('index', array('op' => 'setting', 'type' => 'level')), 'success');
            } else {
                message('删除失败', '', 'error');
            }
        } elseif ($oop == 'changestatus') {
            if ($this->userid == $_W['uid']) {
                $message = array('success' => 0, 'mess' => $this->msg);
                echo json_encode($message);
                exit;
            }
            $id = $_GPC['id'];
            $data['status'] = $_GPC['sta'];
            $info = pdo_get($this->t_level, array('id' => $id, 'weid' => $_W['uniacid']));
            if ($data['status'] == $info['status']) {
                $message = array('success' => 0, '修改失败，请刷新页面后重新修改');
            } else {
                $res = pdo_update($this->t_level, $data, array('id' => $id, 'weid' => $_W['uniacid']));
                if ($res) {
                    $message = array('success' => 1, 'mess' => '修改成功');
                } else {
                    $message = array('success' => 0, 'mess' => '修改失败');
                }
            }
            echo json_encode($message);
            exit;
        }
    } elseif ($type == 'sharetitle') {
        $info = pdo_get($this->t_shartitle, array('weid' => $_W['uniacid']));
        if (checksubmit('submit')) {
            $data['selected'] = $_GPC['selected'];
            $data['mine'] = $_GPC['mine'];
            $data['ranking'] = $_GPC['ranking'];
            $data['ranking_detail'] = $_GPC['ranking_detail'];
            $data['myday'] = $_GPC['myday'];
            $data['rechange'] = $_GPC['rechange'];
            $data['consultation'] = $_GPC['consultation'];
            $data['weid'] = $_W['uniacid'];
            if ($this->userid == $_W['uid']) {
                message($this->msg);
            }
            if (empty($info)) {
                $res = pdo_insert($this->t_shartitle, $data);
            } else {
                $res = pdo_update($this->t_shartitle, $data, array('id' => $info['id']));
            }
            if ($res) {
                message('保存成功', $this->createWebUrl('index', array('op' => 'setting', 'type' => 'sharetitle')), 'success');
            } else {
                message('保存失敗', $this->createWebUrl('index', array('op' => 'setting', 'type' => 'sharetitle')), 'error');
            }
        }
    } elseif ($type == 'classtype') {
        if ($oop == 'list') {
            $contion = "weid = :weid";
            $params = array(':weid' => $_W['uniacid']);
            if ($paixu == 1) {
                $order = " ORDER BY `order` DESC";
            } elseif ($paixu == 2) {
                $order = " ORDER BY `order` ASC";
            }
            $classtypelist = pdo_fetchall("SELECT * FROM " . tablename($this->t_classtype) . " WHERE " . $contion . $order, $params);
        }elseif ($oop == 'sort') {
            if ($this->userid == $_W['uid']) {
                $message = array('success' => 0, 'mess' => $this->msg);
                echo json_encode($message);
                exit;
            }
            $id = $_GPC['classtypeid'];
            $data['order'] = $_GPC['order'];
            $res = pdo_update($this->t_classtype, $data, array('id' => $id, 'weid' => $_W['uniacid']));
            if ($res) {
                $message = array('success' => 1, 'mess' => '修改成功');
            } else {
                $message = array('success' => 0, 'mess' => '修改失败');
            }
        } elseif ($oop == 'add') {
            $classtypeid = $_GPC['classtypeid'];
            $classtypeinfo = pdo_fetch('SELECT * FROM ' . tablename($this->t_classtype) . " WHERE id = :id AND weid = :weid", array(':id' => $classtypeid, ':weid' => $_W['uniacid']));
            if ($_W['isajax']) {
                echo json_encode($classtypeinfo);
                exit;
            }
            if (checksubmit('submit')) {
                $data['name'] = trim($_GPC['name']);
                $data['order'] = intval($_GPC['order']);
                $data['status'] = intval($_GPC['status']);
                $data['weid'] = $_W['uniacid'];
                $data['addtime'] = time();
                if (empty($data['name'])) {
                    message('班型名称不能为空', '', 'error');
                }
                if ($this->userid == $_W['uid']) {
                    message($this->msg);
                }
                if (empty($classtypeid)) {
                    $res = pdo_insert($this->t_classtype, $data);
                    if ($res) {
                        $arr = array(
                            'weid' => $_W['uniacid'],
                            'admin_uid' => $_W['uid'],
                            'admin_username' => $_W['username'],
                            'log_type' => 1,
                            'function' => '基本设置->年级设置',
                            'content' => '新增年级:'.trim($_GPC['name'])
                        );
                        $this->addlog($arr);
                    }
                } else {
                    $res = pdo_update($this->t_classtype, $data, array('weid' => $_W['uniacid'], 'id' => $classtypeid));
                    if ($res) {
                        $arr = array(
                            'weid' => $_W['uniacid'],
                            'admin_uid' => $_W['uid'],
                            'admin_username' => $_W['username'],
                            'log_type' => 3,
                            'function' => '基本设置->年级设置',
                            'content' => '更改年级信息:'.trim($_GPC['name'])
                        );
                        $this->addlog($arr);
                    }
                }
                if ($res) {
                    message('操作成功', $this->createWebUrl('index', array('op' => 'setting', 'type' => "classtype", 'oop' => 'list')), 'success');
                } else {
                    message('操作失败', $this->createWebUrl('index', array('op' => 'setting', 'type' => "classtype", 'oop' => 'list')), 'error');
                }
            }
        } elseif ($oop == 'delete') {
            if ($this->userid == $_W['uid']) {
                message($this->msg);
            }
            $id = intval($_GPC['classtypeid']);
            $snum = pdo_fetch("SELECT * FROM " . tablename($this->t_lesson) . " WHERE classtypeid = :classtypeid AND weid = :weid", array(':classtypeid' => $id, ':weid' => $_W['uniacid']));
            if ($snum) {
                itoast('该班级下还有课程，不能删除');
            }
            $title = pdo_get($this->t_classtype,array('id' => $id),array('name'));
            $res = pdo_delete($this->t_classtype, array('weid' => $_W['uniacid'], 'id' => $id));
            if ($res) {
                $arr = array(
                    'weid' => $_W['uniacid'],
                    'admin_uid' => $_W['uid'],
                    'admin_username' => $_W['username'],
                    'log_type' => 2,
                    'function' => '基本设置->班型设置',
                    'content' => '删除班型:'.$title['name']
                );
                $this->addlog($arr);
                message('删除成功', $this->createWebUrl('index', array('op' => 'setting', 'type' => 'classtype')), 'success');
            } else {
                message('删除失败', '', 'error');
            }
        } elseif ($oop == 'changestatus') {
            if ($this->userid == $_W['uid']) {
                $message = array('success' => 0, 'mess' => $this->msg);
                echo json_encode($message);
                exit;
            }
            $id = $_GPC['id'];
            $data['status'] = $_GPC['sta'];
            $info = pdo_get($this->t_classtype, array('id' => $id, 'weid' => $_W['uniacid']));
            if ($data['status'] == $info['status']) {
                $message = array('success' => 0, '修改失败，请刷新页面后重新修改');
            } else {
                $res = pdo_update($this->t_classtype, $data, array('id' => $id, 'weid' => $_W['uniacid']));
                if ($res) {
                    $message = array('success' => 1, 'mess' => '修改成功');
                } else {
                    $message = array('success' => 0, 'mess' => '修改失败');
                }
            }
            echo json_encode($message);
            exit;
        }
    }
} elseif ($op == 'qiniu') {
    include_once IA_ROOT . '/addons/goouc_class/inc/qiniu/autoload.php';
    $accessKey = 'vCVMJi4EDGv3m-SPXqXoNbosaPRRXRYiMIvN3L1Y';
    $secretKey = 'ByclK9n5NoLtpyydg9rnHbaWnBfTypSGUdAxOkQQ';

    $uploadMgr = new UploadManager();
    $auth = new Auth($accessKey, $secretKey);
    // 构建 UploadManager 对象
    $bucket = 'goouc-xiaohe';
    // 生成上传Token
    $token = $auth->uploadToken($bucket);
    $img = 'images/20/2020/01/vBH8uPuNpHuk8hP48nhf1BZJN5kbjC.jpg';
    $filePath = IA_ROOT . '/attachment/' . $img;
    list($ret, $err) = $uploadMgr->putFile($token, $img, $filePath);

    // $arr = pdo_fetchall("SELECT id,image FROM ".tablename($this->t_lesson)." WHERE weid=:weid ",[':weid'=>$_W['uniacid']]);
    // $arr = pdo_fetchall("SELECT id,image FROM ".tablename($this->t_son).' WHERE weid = :weid',[':weid'=>$_W['uniacid']]);
    // $k = 0;
    // $r = 0;
    // foreach($arr as $key=>$val){
    //     if(!empty($val['image'])){
    //         $token = $auth->uploadToken($bucket);
    //         $filePath = IA_ROOT.'/attachment/'.$val['image'];
    //         list($ret, $err) = $uploadMgr->putFile($token, $val['image'], $filePath);
    //         if($err !== null) {
    //             $r++;
    //         } else {
    //             $k++;
    //         }
    //     }
    // }
    // print_r($k);
    // print_r('<br/>');
    // print_r($r);
    // die();
}elseif($op == 'qnupload'){
    $setting = pdo_get('uni_settings', array('uniacid' => $_W['uniacid']), array('remote'));
    $config = unserialize($setting['remote']);
    if($config['type'] == 3){
        $result = $this->file_dir_remote_uploads(ATTACHMENT_ROOT . 'images/' . $_W['uniacid'],$config['qiniu']);
        message('操作成功', $this->createWebUrl('index'), 'succss');
    }else{
        message('未开启七牛配置', $this->createWebUrl('index'), 'error');
    }
}elseif ($op == 'editname') {
    $pageindex = max(1, $_GPC['page']);
    $psize=1000;
    $contion = "status = :status ";
    $params[':status'] = 1;
    $userlist = pdo_fetchall("SELECT nickname,id FROM " . tablename('goouc_class_user') . " WHERE " . $contion . ' LIMIT ' . ($pageindex - 1) * $psize . ',' . $psize, $params);
    foreach ($userlist as $k=>&$v){
        if(!empty($v['nickname'])){
            pdo_update('goouc_class_user',['nickname'=>json_encode($v['nickname'])],array('id'=>$v['id']));
        }
    }
}
include $this->template('web/setting');
